package com.wemei.auto.ui.base

import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.core.app.ComponentActivity
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.wemei.auto.ui.ext.ACTION_USER_LOGIN
import com.wemei.auto.ui.helper.*
import com.wemei.auto.ui.utils.ReflectUtils

typealias ActivityForResult = (ActivityResult) -> Unit
typealias RequestMultiplePermissions = (Map<String, Boolean>) -> Unit

open class BaseActivity<VB : ViewBinding> : FragmentActivity(), View.OnClickListener, IMsa by msa(), ILoa by loa() {

    val vb by viewBinding(::loadViewBinding)
    protected lateinit var mThis: BaseActivity<VB>

    fun bindViewClick(vararg views: View) = com.wemei.auto.ui.ext.bindViewClick(this, *views)

    open fun loadViewBinding(view: ComponentActivity): VB {
        val clz = ReflectUtils.findParameterizedTypeClz<VB>(this::class.java, 0)
        val method = clz.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as VB
    }

    open fun loadView(load: VB.() -> Unit) = vb.also(load)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initManageStartActivity()
        initLifecycleOwnerActivity()
//        com.wemei.auto.ui.utils.Utils.invasionStatusBar(this)
//        com.wemei.auto.ui.utils.Utils.setDefaultStatusBarFont(this, true)
        mThis = this
        setContentView(vb.root)
        init()
        //RouterServiceBootstrap.getService<IRouterService>().onActivityPostCreated(mThis)
    }

    private fun init() {
        initView()
        initData()
        localRegisterReceiver(ACTION_USER_LOGIN, ::loginChange)
    }

    open fun initView() {}
    open fun initData() {}
    open fun doFinish() {
        finish()
    }

    override fun onClick(v: View?) {

    }

    open fun loginChange(intent: Intent) {

    }

    override fun getResources(): Resources {
//        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources())
        return super.getResources()
    }
}